package com.rocket.bedroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rocket.bedroom.pojo.ClassInfoEntity;
import com.rocket.bedroom.pojo.GradeEntity;
import com.rocket.bedroom.service.IClassInfoService;
import com.rocket.bedroom.service.IGradeService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Generated;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 17:48 2020/5/6
 * @ModifyBy:
 */
@RestController
@RequestMapping(value = "/grade")
public class GradeController {

    @Resource
    private IGradeService gradeService;

    @Resource
    private IClassInfoService classInfoService;

    @GetMapping(value = "/pageGrade")
    public PageInfo<GradeEntity> pageGrade(GradeEntity gradeEntity, Integer pageNumber, Integer pageSize) {

        if (null == pageNumber || 0 >= pageNumber) {
            pageNumber = 1;
        }
        if (null == pageSize || 0 >= pageSize) {
            pageSize = 30;
        }
        PageHelper.startPage(pageNumber, pageSize);


        LambdaQueryWrapper<GradeEntity> lambdaQuery = Wrappers.lambdaQuery();
        if (null != gradeEntity) {
            if (!StringUtils.isEmpty(gradeEntity.getGradeName())) {
                lambdaQuery.likeRight(GradeEntity::getGradeName, gradeEntity.getGradeName());
            }
        }
        List<GradeEntity> list = gradeService.list(lambdaQuery);
        return new PageInfo<>(list);
    }

    @PostMapping(value = "/addGrade")
    public int addGrade(GradeEntity gradeEntity) {
        Assert.notNull(gradeEntity, "ADD grade IS NULL");
        Assert.notNull(gradeEntity.getGradeName(), "ADD grade gradeName IS NULL");
        Assert.isTrue(
                0 == gradeService.hasGradeName(gradeEntity.getGradeName()),
                "添加失败，年级名称存在重复");
        boolean saveGrade = gradeService.save(gradeEntity);
        return saveGrade ? 1 : -1;
    }

    @PutMapping(value = "/modifyGrade")
    public int modifyGrade(GradeEntity gradeEntity) {
        Assert.notNull(gradeEntity, "UPDATE grade IS NULL");
        Assert.notNull(gradeEntity.getGradeName(), "UPDATE grade gradeName IS NULL");
        Assert.notNull(gradeEntity.getGradeId(), "UPDATE grade ID IS NULL");

        /*LambdaQueryWrapper<GradeEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(GradeEntity::getGradeId, gradeEntity.getGradeId());*/
        Assert.isTrue(
                0 == gradeService.hasGradeName(gradeEntity.getGradeName()),
                "修改失败，年级名称存在重复");
        boolean saveGrade = gradeService.updateById(gradeEntity);
        return saveGrade ? 1 : -1;
    }

    @DeleteMapping(value = "/delGradeById/{id}")
    public int delGradeById(@PathVariable("id") Long gradeId) {
        Assert.notNull(gradeId, "DELETE gradeId ID IS NULL");
        Assert.isTrue(
                0 == classInfoService.getClassInfoCount(new ClassInfoEntity(gradeId)),
                "删除年级失败，年级下存在班级信息");
        boolean delGrade = gradeService.removeById(gradeId);
        return delGrade ? 1 : -1;
    }

    @GetMapping(value = "/hasGrade/{gradeName}")
    public int hasGrade(@PathVariable("gradeName") String gradeName) {
        Assert.notNull(gradeName, "DELETE Grade gradeName IS NULL");
        return gradeService.hasGradeName(gradeName);
    }

    @GetMapping(value = "/findGrade/{gradeId}")
    public GradeEntity findGrade(@PathVariable("gradeId") Long gradeId) {
        Assert.notNull(gradeId, "FIND gradeId IS NULL");
        return gradeService.getById(gradeId);
    }


}
